#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    this->setWindowTitle(QObject::tr("登陆界面"));
//####################
//创建一个Qcomobox部件显示账号类型那一行
//###################
    QFont font("Microsoft YaHei",12,75);
    QFont font_line("宋体",12,30);
        type=new QComboBox(this);
        type->addItem(QObject::tr("医护人员账号"));
        type->addItem(QObject::tr("患者账号"));
        type->setGeometry(10,10,130,30);
        type->setFont(font_line);
        account_lable=new QLabel(this);
        account_lable->setText(QObject::tr("账号："));
        account_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
        account_lable->setGeometry(30,100,100,30);
        account_lable->setFont(font);
        account_line=new QLineEdit(this);
        account_line->setGeometry(130,100,200,30);
        account_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
        account_line->setFont(font_line);
        passwd_lable=new QLabel(this);
        passwd_lable->setText(QObject::tr("密码："));
        passwd_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
        passwd_lable->setGeometry(30,150,100,30);
        passwd_lable->setFont(font);
        passwd_line=new QLineEdit(this);
        passwd_line->setGeometry(130,150,200,30);
        passwd_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
        passwd_line->setFont(font_line);

        Login_ptn=new QPushButton(this);
        Login_ptn->setText(QObject::tr("登陆"));
        Login_ptn->setGeometry(60,230,100,30);
        signup_ptn=new QPushButton(this);
        signup_ptn->setText(QObject::tr("注册"));
        signup_ptn->setGeometry(250,230,100,30);
        connect(Login_ptn,&QPushButton::clicked,this,&Login::on_Login_ptn_clicked);
        connect(signup_ptn,&QPushButton::clicked,this,&Login::signup_ptn_clicked);
       this->setFixedSize(400,300);

}

Login::~Login()
{
    // delete ui;
}

int Login::DATABASE_Login()
{

    QString type_text;
    QSqlQuery query(db1);
    type_text=type->currentText();
    if(type->currentIndex()==0)
        type_text=tr("worker");
    else if(type->currentIndex()==1)
        type_text=tr("customer");
        if(account_line->text()!=NULL)
            Account_text=account_line->text();
        else
        {
            QMessageBox::warning(NULL,"ERROR","请输入姓名",QMessageBox::Ok);
            account_line->setFocus();
            return 0;
        }
        if(passwd_line->text()!=NULL)
            Passed_text=passwd_line->text();
        else
        {
            QMessageBox::warning(NULL,"ERROR","请输入密码",QMessageBox::Ok);
            passwd_line->setFocus();
            return 0;
        }

        if(!MYSQL_CON())
        {
            QMessageBox::warning(NULL,"CENNECT DATABASE ERROR","连接失败",QMessageBox::Ok);
            return 0;
        }
        query=QSqlQuery(db1);//对QSqlQuery::exec: database not open错误的解决方案,注意：：：一定要在连接之后才能调用，因为连接数据库之后，db1才是连接好的。
        if(!query.exec("select * from Patient_Login_Hospital"))
        {
            qDebug()<<"搜索出错"<<query.lastError();
            QMessageBox::warning(NULL,"SOURCH CLOUMNS ERROR","搜索出错",QMessageBox::Ok);
            return 0;
        }
        while(query.next())
        {
            if(type_text==query.value(2).toString()&& Account_text==query.value(3).toString()&&Passed_text==query.value(7).toString())
            {
                db1.close();
                QString CON_NAME=db1.connectionName();
                db1.removeDatabase(CON_NAME);
                return 1;
            }
            qDebug()<<"type="<<query.value(2).toString()<< "name="<<query.value(3).toString()<<"passwd="<<query.value(7).toString();
            qDebug()<<"TYPE"<<type_text<< "name="<<Account_text<<"passwd="<<Passed_text;

        }

            QMessageBox::warning(NULL,"INCROOECT","用户名或密码或账户类型不正确，请重新输入",QMessageBox::Ok);
            account_line->clear();
            passwd_line->clear();
            account_line->setFocus();
            return 0;







}

int Login::MYSQL_CON()
{
    db1=QSqlDatabase::addDatabase("QMYSQL","Login_OK");
     qDebug() << "MYSQL driver valid" << db1.isValid();
              db1.setHostName("2001:da8:270:2018:f816:3eff:fe1d:bb01");
              db1.setPort(3306);
              db1.setDatabaseName("MQTT_DATA");
              db1.setUserName("root");
              db1.setPassword("lama940225");
              if(!db1.open())
              {
                  qDebug()<<"打开数据库失败！"<<db1.lastError();
                  return false;
              }

              return true;
}

void Login::on_Login_ptn_clicked()
{
    if(DATABASE_Login())
    {
        if(type->currentIndex()==0)
        {
            doctor=new Medical();
            doctor->name_text =Account_text;
            doctor->passwd_text=Passed_text;
            this->close();
            doctor->show();
        }else if(type->currentIndex()==1)
        {
            P=new patient(0,account_line->text(),passwd_line->text());//自定义构造函数，在列表中初始化两个lineedit内容



            this->close();
            P->show();
        }

    }else
    {
        db1.close();
        QString CON_NAME=db1.connectionName();
        db1.removeDatabase(CON_NAME);
    }


}

void Login::signup_ptn_clicked()
{
    S=new sign_up();
    this->close();
    S->show();
}
